<?php
session_start();
if(isset($_SESSION['admin_id'])) {
    include "../../include/dbFunctions.php";
    include("../../include/mail/mailFunctions.php");
    $id_array = $_POST['id'];
    $event_id = $_SESSION['eid'];
//empty check
    if (!isset($_POST['id'])) {
        $_SESSION['mes']="You must choose at least a volunteer";
    } else {
//update event status
        $query1 = "UPDATE event SET event_status='pending' WHERE event_id=$event_id";
        $result1 = executeQuery($query1);
//update volunteer status and send email
        $sent = 0;
        for($i=0;$i<count($id_array);$i++) {
            $id = $id_array[$i];
            $query4 = "DELETE from trainer WHERE volunteer_id=$id AND event_id = $event_id";
            $result4 = executeSelectQuery($query4);
            $query2 = "INSERT INTO trainer (event_id, volunteer_id, trainer_status) VALUES ('".$event_id."','".$id."','accept')";
            $result2 = executeQuery($query2);
            if ($result2) {
                $_SESSION['mes']="Volunteer has been assigned sucessfully to this event. No email was sent.";
            } else {
                $_SESSION['mes']="Error! Please try again";
            }
        }
    }
header('Location: ' . $_SERVER['HTTP_REFERER']);
    ?>
    <?php
}else {
    header("Location: ../include/login.html");
}
?>
